package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

public class Songyuan163BbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://www.songyuan163.com/.*/.*"
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = url;
		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("div.bbs_page_title").text();
		if (StringUtils.isNotEmpty(title)){
			title = StringUtils.substringBeforeLast(title, "_");
			title = StringUtils.substringBeforeLast(title, "_");
			title = StringUtils.replace(title, "【图】","");
		}

		clickNum = doc.select("#x-views").text();
		commnetNum = doc.select("#x-replys").text();
		date =  DateUtils.matchDate( doc.select("div.bbs_page_title").text());
		content = HtmlCleaner.getContentHtml(url, doc.select("div.bbs_page_content"));
		Element first = doc.select("div.pi").first();
		if(first!=null){
			author = first.text();
		}
			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(4);
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	


	
}
